package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SyncServer;
import com.ruoyi.system.service.ISyncServerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务器管理Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/datasync/server")
public class SyncServerController extends BaseController
{
    @Autowired
    private ISyncServerService syncServerService;

    /**
     * 查询服务器列表
     */
    @PreAuthorize("@ss.hasPermi('system:datasync:server:list')")
    @GetMapping("/list")
    public TableDataInfo list(SyncServer syncServer)
    {
        startPage();
        List<SyncServer> list = syncServerService.selectSyncServerList(syncServer);
        return getDataTable(list);
    }

    /**
     * 导出服务器列表
     */
    @PreAuthorize("@ss.hasPermi('system:datasync:server:export')")
    @Log(title = "服务器管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SyncServer syncServer)
    {
        List<SyncServer> list = syncServerService.selectSyncServerList(syncServer);
        ExcelUtil<SyncServer> util = new ExcelUtil<SyncServer>(SyncServer.class);
        return util.exportExcel(list, "服务器数据");
    }

    /**
     * 获取服务器详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:datasync:server:query')")
    @GetMapping(value = "/{serverId}")
    public AjaxResult getInfo(@PathVariable("serverId") Long serverId)
    {
        return AjaxResult.success(syncServerService.selectSyncServerById(serverId));
    }

    /**
     * 新增服务器
     */
    @PreAuthorize("@ss.hasPermi('system:datasync:server:add')")
    @Log(title = "服务器管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SyncServer syncServer)
    {
        return toAjax(syncServerService.insertSyncServer(syncServer));
    }

    /**
     * 修改服务器
     */
    @PreAuthorize("@ss.hasPermi('system:datasync:server:edit')")
    @Log(title = "服务器管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SyncServer syncServer)
    {
        return toAjax(syncServerService.updateSyncServer(syncServer));
    }

    /**
     * 删除服务器
     */
    @PreAuthorize("@ss.hasPermi('system:datasync:server:remove')")
    @Log(title = "服务器管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serverIds}")
    public AjaxResult remove(@PathVariable Long[] serverIds)
    {
        return toAjax(syncServerService.deleteSyncServerByIds(serverIds));
    }
    
    /**
     * 测试服务器连接
     */
    @PreAuthorize("@ss.hasPermi('system:datasync:server:test')")
    @Log(title = "服务器管理", businessType = BusinessType.OTHER)
    @GetMapping("/test/{serverId}")
    public AjaxResult testConnection(@PathVariable("serverId") Long serverId)
    {
        try
        {
            return syncServerService.testConnection(serverId) ? success("连接成功") : error("连接失败");
        }
        catch (Exception e)
        {
            return error("连接失败: " + e.getMessage());
        }
    }
} 